import Adsync
import TrPinyin
from Unit import Unit
from UnitSub import getPriUnit
from Account import Account

# 获取今日域控人员名单
# [{'workno': '29735', 'name': '夏晶', 'dept': '汽车产线事业部'}, ...]
newcomer_list = Adsync.Adsync()
print(newcomer_list)
unit_obj = Unit()
account_obj = Account()


# 账号重名处理
def Handle_repeatName(account_name):
    repeat_count = 0
    while True:
        is_repeat = unit_obj.getAccountList_account(account_name)['con']['count']
        if is_repeat != 0:
            # 如果重名添加数字
            repeat_count = repeat_count + 1
            account_name = account_name.split('0')[0] + '0' + str(repeat_count)
        else:
            return account_name


for newcomer_dic in newcomer_list:
    # 生成邮箱账号名
    account_name = TrPinyin.TrPinyin(newcomer_dic['name'])
    # 判断是否重名
    account_name = Handle_repeatName(account_name)
    print(account_name)
    # 获取工号
    job_no = newcomer_dic['workno']
    # 获取姓名
    nickname = newcomer_dic['name']
    # 判断该工号是否已有邮箱账号
    is_exist = unit_obj.getAccountList_jobno(job_no)
    # if is_exist['con']['count']:
    #     print(account_name, newcomer_dic['dept'])
    #     print(getPriUnit(newcomer_dic['dept']))
        # 创建账号
        # print(account_obj.createAccount(account_name, job_no, nickname, unit_id='default'))
        # 绑定邮件收发限制策略
        # print(account_obj.updateRsLimit(account_name, '-1'))
